<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\IndexService;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    //首页

    public function __construct(IndexService $service)
    {
        $this->db = $service;
    }

    //登录
    public function dologin(Request $request)
    {
//        $this->validate($request, [
//            'uname' => 'required|min:2|max:255',
//            'upwd' => 'required|min:6|max:20',
//        ], [
//            'uname.required' => '账户名称必须的',
//            'upwd.required' => '密码必须的',
//            'upwd.min' => '密码不少于 :min 位',
//            'upwd.max' => '密码不多于 :max 位',
//        ]);
        $uname = $request->post('uname', '');
        $upwd = $request->post('upwd', '');
        if (mb_strlen($uname) < 2 || mb_strlen($uname) > 255) {
            return $this->toJson(-2, '账号名必须2到255位字符之间');
        }
        if (strlen($upwd) < 6 || strlen($upwd) > 20) {
            return $this->toJson(-2, '密码必须6到20位字符之间');
        }
        if (!preg_match('/[0-9a-zA-Z*#&_]{6,20}/', $upwd)) {
            return $this->toJson(-2, '密码格式错误');
        }
        $res = (new IndexService())->dologin($uname, $upwd);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //登录日志
    public function loginlog(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $token = $request->post('token', '');
        if (!$token) {
            return $this->toJson(-2, '没有登录');
        }
        $res = (new IndexService())->getLoginLog($token, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //获取左侧菜单
    public function leftMenu(Request $request)
    {
        $token = $request->post('token', '');
        $checkLogin = (new IndexService())->checkLogin($token);
        if ($checkLogin['code'] < 0) {
            return $this->toJson(-3, $checkLogin['msg']);
        }
        $aid = (new IndexService())->aidByToken($token);
        $res = (new IndexService())->getLeftMenu($aid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //首页统计
    public function tongji()
    {
        $res = (new IndexService())->tongji();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //列表
    public function getActionIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new IndexService())->getActionIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //一个
    public function getActionOne(Request $request)
    {
        $id = $request->post('id');
        $res = (new IndexService())->getActionOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //父级
    public function getParent()
    {
        $res = (new IndexService())->getParent();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //保存
    public function actionUpdate(Request $request)
    {
        $id = $request->post('id');
        $pas['title'] = $request->post('title');
        $pas['pid'] = $request->post('pid', 0);
        $pas['link'] = $request->post('link');
        if (!$pas['title']) {
            return $this->toJson(-2, '功能名称必须的');
        }
        $res = (new IndexService())->actionUpdate($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //购物车列表
    public function getCartIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new IndexService())->getCartList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //日志列表
    public function getLogIndex()
    {
        $pathDir = 'logs/';
        $resScans =  scandir($pathDir);
        $logs = [];
        if (count($resScans)>=3) {
            foreach ($resScans as $dir) {
                $dirInfo = pathinfo($dir);
                $fileName = $dirInfo['basename'];
                if (!in_array($fileName,['.','..']) && $fileName!='index.html') {
                    $sizex = filesize($pathDir.$fileName);
                    $size = $this->getSizeLevel($sizex);
                    $logs[] = [
                        'filename' => $fileName,
                        'pathname' => '/logs/' . $fileName,
                        'size' => $size,
                    ];
                }
            }
        }
        return $this->toJson(0, '', $logs);
    }

    /**
     * 首页数据看板
     */
    public function indexTongji()
    {
        $res = $this->db->indexTongji();
        return $this->toJson(0, '', $res);
    }

    /**
     * 财务数据看板
     */
    public function caiwuTongji(Request $request)
    {
        $riqi01 = $request->post('rq01', 0);
        $riqi02 = $request->post('rq02', 0);
        $riqi01 = $this->riqi($riqi01);
        $riqi02 = $this->riqi($riqi02);
        $res = $this->db->caiwuTongji($riqi01, $riqi02);
        return $this->toJson(0, '', $res);
    }

    //获取日期时间
    public function riqi($rq)
    {
        if ($rq == 1) { //昨日
            $day = date('Y-m-d', strtotime("-1 day"));
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else if ($rq == 2) { //今日
            $day = date('Y-m-d', time());
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else if ($rq == 3) { //三天前
            $day = date('Y-m-d', strtotime("-3 days"));
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else if ($rq == 4) { //七天前
            $day = date('Y-m-d', strtotime("-7 days"));
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else if ($rq == 5) { //一个月前
            $day = date('Y-m-d', strtotime("-1 month", time()));
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else if ($rq == 6) { //三个月前
            $day = date('Y-m-d', strtotime("-3 months", time()));
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else if ($rq == 7) { //半年前
            $day = date('Y-m-d', strtotime("-6 months", time()));
            return [
                'start' => date('Y-m-d H:i:s', strtotime($day.' 00:00:01')),
                'end' => date('Y-m-d H:i:s', strtotime($day.' 23:59:59')),
            ];
        } else {
            return [
                'start' => '', 'end' => '',
            ];
        }
    }
}